package ruifeng.zcj.dig.ui.shopping.shoppinglist;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bzh.recycler.ExRecyclerView;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;

import org.greenrobot.eventbus.EventBus;

import java.io.Serializable;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewAdapter;
import cn.bingoogolapple.androidcommon.adapter.BGARecyclerViewHolder;
import cn.bingoogolapple.androidcommon.adapter.BGAViewHolderHelper;
import ruifeng.zcj.dig.R;
import ruifeng.zcj.dig.base.DepositFragmentActivity;
import ruifeng.zcj.dig.base.MyApp;
import ruifeng.zcj.dig.base.api.ApiHttpClient;
import ruifeng.zcj.dig.base.api.OSChinaApi;
import ruifeng.zcj.dig.base.bean.Base;
import ruifeng.zcj.dig.groble.FragmentArgs;
import ruifeng.zcj.dig.groble.ToActivityUtil;
import ruifeng.zcj.dig.ui.base.RefreshRecyclerFragment;
import ruifeng.zcj.dig.ui.shopping.ShoppingListActivity;
import ruifeng.zcj.dig.ui.shopping.bean.ShoppingList;
import ruifeng.zcj.dig.ui.shopping.bean.ThreeCategory;
import ruifeng.zcj.dig.ui.shopping.shoppingdetail.ShoppingDetailActivity;
import ruifeng.zcj.mybase.base.utils.GlideUtil;
import ruifeng.zcj.mybase.base.utils2.AbJsonUtil;

/**
 * 查看所有商品
 * Created by Administrator on 2016/9/30 0030.
 */
public class ShoppingSelectFragment extends RefreshRecyclerFragment {
    private static final String CACHE_KEY_PREFIX = "ShoppingSelectFragment";
    ThreeCategory.RowsBean rowsBean = null;
    MyApp mMyApp = null;
    ShoppingListActivity mShoppingListActivity;

    protected ShoppingList mShoppingList;
    @Bind(R.id.defaults_test)
    TextView mDefaultsTest;
    @Bind(R.id.defaults)
    RelativeLayout mDefaults;
    @Bind(R.id.volume)
    TextView mVolume;
    @Bind(R.id.volume_img)
    ImageView mVolumeImg;
    @Bind(R.id.volumes_rela)
    RelativeLayout mVolumesRela;
    @Bind(R.id.prives_test)
    TextView mPrivesTest;
    @Bind(R.id.prives)
    ImageView mPrives;
    @Bind(R.id.prives_rela)
    RelativeLayout mPrivesRela;


    @Bind(R.id.refreshLayout)
    protected SwipeRefreshLayout swipeRefreshLayout;

    @Bind(R.id.recyclerView)
    protected ExRecyclerView recyclerView;




    @Override
    protected int getContentView() {
        return R.layout.fragment_shopping_select;
    }

    @Override
    protected void initFragmentConfig(View view) {
    //     super.initFragmentConfig(view);

        if (recyclerView == null) {
            // recyclerView = (ExRecyclerView) view.findViewById(R.id.recyclerView);
            recyclerView = (ExRecyclerView) getViewById(R.id.recyclerView);
        }
        if (OnLoadingMore()) {
            recyclerView.setOnLoadingMoreListener(this);
        }

        if (getRefreshable()) {
            swipeRefreshLayout.setOnRefreshListener(this);
            swipeRefreshLayout.setColorSchemeResources(android.R.color.holo_blue_bright,
                    android.R.color.holo_green_light,
                    android.R.color.holo_orange_light,
                    android.R.color.holo_red_light);
        }

        mMomentAdapter = getAdapter();

        mMomentAdapter.setOnRVItemClickListener(this);
        mMomentAdapter.setOnRVItemLongClickListener(this);
        recyclerView.setAdapter(mMomentAdapter);



        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mShoppingListActivity = (ShoppingListActivity) mActivity;

        mMyApp = (MyApp) mApp;
    }

    public String getSumcierid() {
        if (rowsBean != null) return rowsBean.getSumcierid();
        return null;
    }


    @Override
    protected BGARecyclerViewAdapter getAdapter() {
        return new MomentAdapter(recyclerView);
    }

    @Override
    protected String getCacheKeyPrefix() {
        if (rowsBean != null) {
            String name = rowsBean.getSumciertype().replace("/", "_").replace("、", "_");
            StringBuffer append = new StringBuffer();
            append.append(name);
            append.append(rowsBean.getSumcierid());
            return rowsBean.getSumcierid() + CACHE_KEY_PREFIX + append;
        }
        return CACHE_KEY_PREFIX;
    }

    @Override
    public void onRVItemClick(ViewGroup parent, View itemView, int position) {
        if (mShoppingList != null) {

            ShoppingList.RowsBean rowsBean = mShoppingList.getRows().get(position);
            // 发送一个对象给另一个Fragment
         //   EventBus.getDefault().post(rowsBean);
            FragmentArgs fragmentArgs2 = new FragmentArgs();
            fragmentArgs2.add("ShoppingList", rowsBean);
            ToActivityUtil.toNextActivity(getContext(), ShoppingDetailActivity.class, "ShoppingListBundle", fragmentArgs2);

        }
    }

    @Override
    public boolean onRVItemLongClick(ViewGroup parent, View itemView, int position) {
        return false;
    }

    @Override
    protected void onUserVisible() {

    }

    @Override
    protected long getAutoRefreshTime() {
        // 最新商品分类两小时刷新一次
        return 2 * 60 * 60;
    }

    protected void sendRequestDatas(int id) {// 默认的一个用户的id等于6了
        OSChinaApi.getgoodslist(getSumcierid(), id, "6", new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (!TextUtils.isEmpty(responseInfo.result)) {
                    sendRequestDataSucess(responseInfo.result);
                    if (isAdded()) {
                        if (mState == STATE_REFRESH) {
                            onRefreshNetworkSuccess();
                        }
                        Base base = fromJson(responseInfo.result);
                        // 保存数据
                        saveObject(getCacheKey(), base);
                        executeOnLoadDataSuccess(base);// 读取数据成功
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {

            }
        });
    }

    /****
     * 网络请求数据具体的子类实现
     **/
    protected void sendRequestData() {
        sendRequestDatas(0);
    }

    /***
     * 把数据显示到view上面去
     **/
    @Override
    protected void executeOnLoadDataSuccess(Serializable seri) {
        super.executeOnLoadDataSuccess(seri);
        mShoppingList = (ShoppingList) seri;
        if (mMomentAdapter != null && mShoppingList != null) {
            mMomentAdapter.addNewData(mShoppingList.getRows());
        }
    }

    protected Base fromJson(String netResult) {
        return (ShoppingList) AbJsonUtil.fromJson(netResult, ShoppingList.class);
    }



    public class MomentAdapter extends BGARecyclerViewAdapter<ShoppingList.RowsBean> {
        public MomentAdapter(RecyclerView recyclerView) {
            super(recyclerView, R.layout.listview_item_vehicle);
        }

        @Override
        public void fillData(BGAViewHolderHelper helper, int position, ShoppingList.RowsBean moment) {
            /*if (TextUtils.isEmpty(moment.content)) {
                helper.setVisibility(R.id.tv_item_moment_content, View.GONE);
            } else {
                helper.setVisibility(R.id.tv_item_moment_content, View.VISIBLE);
                helper.setText(R.id.tv_item_moment_content, moment.content);
            }*/

            ImageView imageView = helper.getImageView(R.id.vehicle_fragment_item_img);

            GlideUtil.loadImage(mContext, ApiHttpClient.getAbsoluteApiUrl(moment.getPhoto()), imageView);


            helper.setText(R.id.vehicle_fragment_item_title, "" + moment.getGoodsname());
            helper.setText(R.id.vehicle_fragment_item_address, "销量:" + moment.getSales());
            helper.setText(R.id.vehicle_fragment_item_price1, "价格");
            helper.setText(R.id.vehicle_fragment_item_price, "￥ " + moment.getNewprice());


            helper.setText(R.id.vehicle_fragment_item_text_time, "￥" + moment.getOldprice());

            // View  view = helper.getView(R.id.vehicle_fragment_item_img);

            // TextView textView = helper.getTextView(R.id.vehicle_fragment_item_img);
            //    ViewUtils.setTextViewLineFlag((AppCompatImageView) view,Paint.STRIKE_THRU_TEXT_FLAG| Paint.ANTI_ALIAS_FLAG);

         /*   ImageView imageView = helper.getImageView(R.id.vehicle_fragment_item_img);

            GlideUtil.loadImage(mContext, String.format("http://kw.runfkj.com%s", moment.getPhoto()), imageView);

            helper.setText(R.id.vehicle_fragment_item_title, " " + moment.getSumciertype());
            //  helper.setText(R.id.vehicle_fragment_item_address, "齿轮总成/星星齿");
            helper.setVisibility(R.id.vehicle_fragment_item_address, View.GONE);
*/


          /*
            /*TextView postingsname = helper.getView(R.id.forum_fragment_item_text_title);

            BGANinePhotoLayout ninePhotoLayout = helper.getView(R.id.npl_item_moment_photos);

            ninePhotoLayout.init(MomentListActivity.this);
            ninePhotoLayout.setDelegate(MomentListActivity.this);
            ninePhotoLayout.setData(moment.photos);*/
        }

        @Override
        public void onViewRecycled(BGARecyclerViewHolder holder) {
            super.onViewRecycled(holder);

           /* BGANinePhotoLayout ninePhotoLayout = holder.getViewHolderHelper().getView(R.id.npl_item_moment_photos);
            ninePhotoLayout.init(null);
            ninePhotoLayout.setDelegate(null);*/
        }
    }

    @OnClick({R.id.defaults, R.id.volumes_rela, R.id.prives_rela})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.defaults:
                sendRequestDatas(0);
                break;
            case R.id.volumes_rela:
                sendRequestDatas(1);
                break;
            case R.id.prives_rela:
                sendRequestDatas(2);
                break;
        }
    }

    @Override
    protected void onFirstUserVisible() {
       super.onFirstUserVisible();
        rowsBean = mShoppingListActivity.rowsBean;


    }
}
